package com.tju.resource.oauth.controller.tb;

import com.tju.resource.common.ServiceException;
import com.tju.resource.model.TbOutward;
import com.tju.resource.model.TbOutwardConsumables;
import com.tju.resource.server.TbOutwardService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

@RestController
public class TbOutwardController {

    public static final String prefix = "oward";

    @Autowired
    TbOutwardService service;


    /**
     * 分页查询外发记录
     *
     * @param params
     * @return
     */
    @PostMapping(prefix + "/getAll")
    @PreAuthorize("hasAuthority('sys:goods:oward:list')")
    public Result<Object> getAll(@RequestBody Map<String, String> params) {
        return service.getAll(params);
    }


    /**
     * 添加外发信息
     *
     * @param outward
     * @return
     */
    @PostMapping(prefix + "/add")
    @PreAuthorize("hasAuthority('sys:goods:oward:add')")
    public Result<Object> add(@Valid @RequestBody TbOutward outward, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
            } else
                return service.add(outward);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改外发信息
     *
     * @param outward
     * @return
     */
    @PostMapping(prefix + "/upd")
    @PreAuthorize("hasAuthority('sys:goods:oward:edit')")
    public Result<Object> upd(@Valid @RequestBody TbOutward outward, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
            } else
                return service.upd(outward);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 删除外发信息
     *
     * @param outward
     * @return
     */
    @PostMapping(prefix + "/del")
    @PreAuthorize("hasAuthority('sys:goods:oward:delete')")
    public Result<Object> del(@RequestBody TbOutward outward) {
        try {
            return service.del(outward.getId());
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }


    /**
     * 审核外发
     *
     * @param outward
     * @return
     */
    @PostMapping(prefix + "/check")
    @PreAuthorize("hasAuthority('sys:goods:oward:approve')")
    public Result<Object> check(@RequestBody TbOutward outward) {
        try {
            return service.check(outward);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 外发单添加,修改耗材物品
     *
     * @param tbOutwardConsumables
     * @return
     */
    @PostMapping(prefix + "/managementConsumables")
    @PreAuthorize("hasAuthority('sys:goods:oward:add')")
    public Result<Object> managementConsumables(@RequestBody TbOutwardConsumables tbOutwardConsumables) {
        return service.managementConsumables(tbOutwardConsumables);
    }

    /**
     * 审核外发
     *
     * @param outward
     * @return
     */
    @PostMapping(prefix + "/oneAudit")
    @PreAuthorize("hasAuthority('sys:goods:oward:approve')")
    public Result<Object> oneAudit(@RequestBody TbOutward outward) {
        try {
            return service.oneAudit(outward);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
